import axios from "axios";
import router from "@/router";
import { Element, Message } from "element-ui";



axios.defaults.baseURL = "http://localhost:8081"
axios.async = true
const request = axios.create({
    timeout: 5000,
    headers: {
        'Content-Type': "application/json;charset=utf-8"
    },

})

request.interceptors.request.use(config => {
    config.headers["Authorization"] = localStorage.getItem("token")
    return config
})


request.interceptors.response.use(response => {
    console.log(response);
    debugger
    let res = response.data;
    //需要判断这个code这个状态码，如果后端返回的这个状态码是200.正常放行
    if (res.code === 200) {
        return response
    } else {
        Message.error(res.msg ? res.msg : '系统异常')
        //    Element.Message.error(res.msg?res.msg:'系统异常')
        //    return Promise.reject(response.data.msg)
    }
}, error => {
    //这块代码是处理的业务异常
    if (error.response.data) {
        error.massage = error.response.data.msg
    }
    //401代表的是没有权限，需要重新进入到登录界面
    if (error.response.status === 401) {
        router.push("/login")
    }
    Message.error(error.message, { duration: 3000 })
}
)

export default request